package cn.iocoder.yudao.module.worker.service.category;

import cn.iocoder.yudao.module.worker.controller.admin.goods.vo.WorkerGoodsPageReqVO;
import cn.iocoder.yudao.module.worker.controller.app.category.vo.AppCategoryPageReqVO;
import cn.iocoder.yudao.module.worker.controller.app.category.vo.AppCategorySaveReqVO;
import cn.iocoder.yudao.module.worker.dal.dataobject.goods.WorkerGoodsDO;
import cn.iocoder.yudao.module.worker.dal.mysql.goods.WorkerGoodsMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.worker.controller.admin.category.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.category.CategoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.worker.dal.mysql.category.CategoryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.worker.enums.ErrorCodeConstants.*;

/**
 * 商品分类 Service 实现类
 *
 * @author Jerry
 */
@Service
@Validated
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private WorkerGoodsMapper workerGoodsMapper;

    @Override
    public Long createCategory(CategorySaveReqVO createReqVO) {
        // 插入
        CategoryDO category = BeanUtils.toBean(createReqVO, CategoryDO.class);
        categoryMapper.insert(category);
        // 返回
        return category.getId();
    }

    @Override
    public void updateCategory(CategorySaveReqVO updateReqVO) {
        // 校验存在
        validateCategoryExists(updateReqVO.getId());
        // 更新
        CategoryDO updateObj = BeanUtils.toBean(updateReqVO, CategoryDO.class);
        categoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteCategory(Long id) {
        // 校验存在
        validateCategoryExists(id);

        // 请先删除子类
        CategoryPageReqVO categoryReqVO = new CategoryPageReqVO();
        categoryReqVO.setParentId(id);
        List<CategoryDO> categoryList = categoryMapper.selectList(categoryReqVO);
        if(!CollectionUtils.isEmpty(categoryList)){
            throw exception(CATEGORY_CHILDREN_EXISTS);
        }

        // 关联商品不能删除
        WorkerGoodsPageReqVO workerGoodsReqVO = new WorkerGoodsPageReqVO();
        workerGoodsReqVO.setCategoryId(id);
        List<WorkerGoodsDO> goodsList = workerGoodsMapper.selectList(workerGoodsReqVO);
        if(!CollectionUtils.isEmpty(goodsList)){
            throw exception(CATEGORY_GOODS_EXISTS);
        }
        // 删除
        categoryMapper.deleteById(id);
    }

    private void validateCategoryExists(Long id) {
        if (categoryMapper.selectById(id) == null) {
            throw exception(CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public CategoryDO getCategory(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public PageResult<CategoryDO> getCategoryPage(CategoryPageReqVO pageReqVO) {
        return categoryMapper.selectPage(pageReqVO);
    }

    @Override
    public Long createCategory(AppCategorySaveReqVO createReqVO) {
        // 插入
        CategoryDO category = BeanUtils.toBean(createReqVO, CategoryDO.class);
        categoryMapper.insert(category);
        // 返回
        return category.getId();
    }

    @Override
    public void updateCategory(AppCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateCategoryExists(updateReqVO.getId());
        // 更新
        CategoryDO updateObj = BeanUtils.toBean(updateReqVO, CategoryDO.class);
        categoryMapper.updateById(updateObj);
    }

    @Override
    public PageResult<CategoryDO> getCategoryPage(AppCategoryPageReqVO pageReqVO) {
        return categoryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CategoryDO> getCategoryList(CategoryPageReqVO listReqVO) {
        return categoryMapper.selectList(listReqVO);
    }

    @Override
    public void updateCategoryStatus(Long id, Boolean status) {
        CategoryDO categoryDO = categoryMapper.selectById(id);
        categoryDO.setStatus(status);
        categoryMapper.updateById(categoryDO);
    }

    @Override
    public void updateCategoryBlindStatus(Long id, Boolean blindStatus) {
        CategoryDO categoryDO = categoryMapper.selectById(id);
        categoryDO.setBlindStatus(blindStatus);
        categoryMapper.updateById(categoryDO);
    }

    @Override
    public List<CategoryDO> getAppCategoryList(AppCategoryPageReqVO categoryReqVO) {
        return categoryMapper.selectList(categoryReqVO);
    }


}